home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 204_01 / decl.c < prev    next >
Text File  |  1980-01-01  |  17KB  |  487 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. TYP             *head = 0;
  26. TYP             *tail = 0;
  27. char            *declid = 0;
  28. TABLE           tagtable = {0,0};
  29. TYP             stdconst = { bt_long, 1, 4, {0, 0}, 0, "stdconst"};
  30.  
  31. int     imax(i,j)
  32. int     i,j;
  33. {       return (i > j) ? i : j;
  34. }
  35.  
  36. char    *litlate(s)
  37. char    *s;
  38. {       char    *p;
  39.         p = xalloc(strlen(s) + 1);
  40.         strcpy(p,s);
  41.         return p;
  42. }
  43.  
  44. TYP     *maketype(bt,siz)
  45. int     bt, siz;
  46. {       TYP     *tp;
  47.         tp = xalloc(sizeof(TYP));
  48.         tp->val_flag = 0;
  49.         tp->size = siz;
  50.         tp->type = bt;
  51.         tp->sname = 0;
  52.         tp->lst.head = 0;
  53.         return tp;
  54. }
  55.  
  56. int     decl(table)
  57. TABLE   *table;
  58. {       switch (lastst) {
  59.                 case kw_char:
  60.                         head = tail = maketype(bt_char,1);
  61.                         getsym();
  62.                         break;
  63.                 case kw_short:
  64.                         head = tail = maketype(bt_short,2);
  65.                         getsym();
  66.                         break;
  67.                 case kw_int: case kw_long:
  68.                         head = tail = maketype(bt_long,4);
  69.                         getsym();
  70.                         break;
  71.                 case kw_unsigned:
  72.                         head = tail = maketype(bt_unsigned,4);
  73.                         getsym();
  74.                         if( lastst == kw_int )
  75.                                 getsym();
  76.                         break;
  77.                 case id:                /* no type declarator */
  78.                         head = tail = maketype(bt_long,4);
  79.                         break;
  80.                 case kw_float:
  81.                         head = tail = maketype(bt_float,4);
  82.                         getsym();
  83.                         break;
  84.                 case kw_double:
  85.                         head = tail = maketype(bt_double,8);
  86.                         getsym();
  87.                         break;
  88.                 case kw_enum:
  89.                         getsym();
  90.                         declenum(table);
  91.                         break;
  92.                 case kw_struct:
  93.                         getsym();
  94.                         declstruct(bt_struct);
  95.                         break;
  96.                 case kw_union:
  97.                         getsym();
  98.                         declstruct(bt_union);
  99.                         break;
  100.                 }
  101. }
  102.  
  103. decl1()
  104. {       TYP     *temp1, *temp2, *temp3, *temp4;
  105.         switch (lastst) {
  106.                 case id:
  107.                         declid = litlate(lastid);
  108.                         getsym();
  109.                         decl2();
  110.                         break;
  111.                 case star:
  112.                         temp1 = maketype(bt_pointer,4);
  113.                         temp1->btp = head;
  114.                         head = temp1;
  115.                         if(tail == NULL)
  116.                                 tail = head;
  117.                         getsym();
  118.                         decl1();
  119.                         break;
  120.                 case openpa:
  121.                         getsym();
  122.                         temp1 = head;
  123.                         temp2 = tail;
  124.                         head = tail = NULL;
  125.                         decl1();
  126.                         needpunc(closepa);
  127.                         temp3 = head;
  128.                         temp4 = tail;
  129.                         head = temp1;
  130.                         tail = temp2;
  131.                         decl2();
  132.                         temp4->btp = head;
  133.                         if(temp4->type == bt_pointer &&
  134.                                 temp4->val_flag != 0 && head != NULL)
  135.                                 temp4->size *= head->size;
  136.                         head = temp3;
  137.                         break;
  138.                 default:
  139.                         decl2();
  140.                         break;
  141.                 }
  142. }
  143.  
  144. decl2()
  145. {       TYP     *temp1;
  146.         switch (lastst) {
  147.                 case openbr:
  148.                         getsym();
  149.                         temp1 = maketype(bt_pointer,0);
  150.                         temp1->val_flag = 1;
  151.                         temp1->btp = head;
  152.                         if(lastst == closebr) {
  153.                                 temp1->size = 0;
  154.                                 getsym();
  155.                                 }
  156.                         else if(head != NULL) {
  157.                                 temp1->size = intexpr() * head->size;
  158.                                 needpunc(closebr);
  159.                                 }
  160.                         else {
  161.                                 temp1->size = intexpr();
  162.                                 needpunc(closebr);
  163.                                 }
  164.                         head = temp1;
  165.                         if( tail == NULL)
  166.                                 tail = head;
  167.                         decl2();
  168.                         break;
  169.                 case openpa:
  170.                         getsym();
  171.                         temp1 = maketype(bt_func,0);
  172.                         temp1->val_flag = 1;
  173.                         temp1->btp = head;
  174.                         head = temp1;
  175.                         if( lastst == closepa) {
  176.                                 getsym();
  177.                                 if(lastst == begin)
  178.                                         temp1->type = bt_ifunc;
  179.                                 }
  180.                         else
  181.                                 temp1->type = bt_ifunc;
  182.                         break;
  183.                 }
  184. }
  185.  
  186. int     alignment(tp)
  187. TYP     *tp;
  188. {       switch(tp->type) {
  189.                 case bt_char:           return AL_CHAR;
  190.                 case bt_short:          return AL_SHORT;
  191.                 case bt_long:           return AL_LONG;
  192.                 case bt_enum:           return AL_SHORT;
  193.                 case bt_pointer:
  194.                         if(tp->val_flag)
  195.                                 return alignment(tp->btp);
  196.                         else
  197.                                 return AL_POINTER;
  198.                 case bt_float:          return AL_FLOAT;
  199.                 case bt_double:         return AL_DOUBLE;
  200.                 case bt_struct:
  201.                 case bt_union:          return AL_STRUCT;
  202.                 default:                return AL_CHAR;
  203.                 }
  204. }
  205.  
  206. int     declare(table,al,ilc,ztype)
  207. /*
  208.  *      process declarations of the form:
  209.  *
  210.  *              <type>  <decl>, <decl>...;
  211.  *
  212.  *      leaves the declarations in the symbol table pointed to by
  213.  *      table and returns the number of bytes declared. al is the
  214.  *      allocation type to assign, ilc is the initial location
  215.  *      counter. if al is sc_member then no initialization will
  216.  *      be processed. ztype should be bt_struct for normal and in
  217.  *      structure declarations and sc_union for in union declarations.
  218.  */
  219. TABLE           *table;
  220. int               al;
  221. int             ilc;
  222. int               ztype;
  223. {       SYM     *sp, *sp1;
  224.         TYP     *dhead;
  225.         int     nbytes;
  226.         nbytes = 0;
  227.         decl(table);
  228.         dhead = head;
  229.         for(;;) {
  230.                 declid = 0;
  231.                 decl1();
  232.                 if( declid != 0) {      /* otherwise just struct tag... */
  233.                         sp = xalloc(sizeof(SYM));
  234.                         sp->name = declid;
  235.                         sp->storage_class = al;
  236.                         while( (ilc + nbytes) % alignment(head)) {
  237.                                 if( al != sc_member &&
  238.